//
//  SeekViewController.m
//  SmartCup
//
//  Created by Kendama on 2017/5/18.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "SeekViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapNaviKit/AMapNaviKit.h>
#import "FIDCheckMap.h"

@interface SeekViewController ()<AMapLocationManagerDelegate,MAMapViewDelegate,AMapNaviWalkManagerDelegate>

@property(nonatomic,strong) AMapLocationManager *locationManager;
/** <#Description#> **/
@property(nonatomic,strong) MAMapView *mapView;

@property(nonatomic,strong) AMapNaviPoint *startPoint;
@property(nonatomic,strong) AMapNaviPoint *endPoint;
@property(nonatomic,strong) AMapNaviWalkManager *walkManager;


@end

@implementation SeekViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //注册通知
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"matchMachine" object:nil];

}

- (void)refreshBaseControlValue {
    [self setNavigationTitle:@"位置记录" titleColor:[UIColor whiteColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"3E76F6"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F9"];
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    [self setRightButtonTitle:@"导航"];
    [self layoutSubviews];
    
    [self initMap];
}

- (void)onRightButtonClick:(id)sender {
//    AMapLocationManager *locationManager = [[AMapLocationManager alloc] init];
//    locationManager.delegate = self;
//    [locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//    //    iOS 9（不包含iOS 9） 之前设置允许后台定位参数，保持不会被系统挂起
//    [locationManager setPausesLocationUpdatesAutomatically:YES];
//    //iOS 9（包含iOS 9）之后新特性：将允许出现这种场景，同一app中多个locationmanager：一些只能在前台定位，另一些可在后台定位，并可随时禁止其后台定位。
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
//        locationManager.allowsBackgroundLocationUpdates = YES;
//    }
//    
//    // 带逆地理（返回坐标和地址信息）。将下面代码中的 YES 改成 NO ，则不会返回地址信息。
//    [locationManager requestLocationWithReGeocode:NO completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
//        if (error)
//        {
//            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
//            if (error.code == AMapLocationErrorLocateFailed)
//            {
//                return;
//            }
//        }
//        NSString *latitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
//        NSString *longitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
//        DTCSAVE_OBJECT(longitude, @"longitude");
//        DTCSAVE_OBJECT(latitude, @"latitude");
//        NSLog(@"location:%f and %f", location.coordinate.longitude,location.coordinate.latitude);
//        if (regeocode)
//        {
//            NSLog(@"reGeocode:%@", regeocode);
//        }
//    }];
    
    CheckMapModel *mapModel = [[CheckMapModel alloc] init];
    mapModel.latitude = [DTCEXRACT_OBJECT(@"latitude") floatValue];
    mapModel.longitude = [DTCEXRACT_OBJECT(@"longitude") floatValue];
//    CheckMapModel *mapModel = [[CheckMapModel alloc] init];
//    mapModel.latitude = 39.99;
//    mapModel.longitude = 116.47;
    
    [FIDCheckMap callMapWithpositionModel:mapModel completion:^(UIAlertController *alertController) {
        [self presentViewController:alertController animated:YES completion:nil];
    }];



}

- (void)walkManagerOnCalculateRouteSuccess:(AMapNaviWalkManager *)walkManager
{
    NSLog(@"onCalculateRouteSuccess");
    
    //显示路径或开启导航
}

- (void) initMap {
//    self.mapView.showsUserLocation = YES;

//    //    self.locationManager.distanceFilter ＝ 200;
//    //    iOS 9（不包含iOS 9） 之前设置允许后台定位参数，保持不会被系统挂起
//    [self.locationManager setPausesLocationUpdatesAutomatically:YES];
//    //iOS 9（包含iOS 9）之后新特性：将允许出现这种场景，同一app中多个locationmanager：一些只能在前台定位，另一些可在后台定位，并可随时禁止其后台定位。
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
//        self.locationManager.allowsBackgroundLocationUpdates = YES;
//    }
//    //    //开始持续定位
//    [self.locationManager startUpdatingLocation];
}

#pragma mark -- 事件
//- (void)notification:(NSNotification *)dic{
//    if ([dic.userInfo[@"blueDisconnect"] isEqualToString:@"YES"]) {
//        [SVProgressHUD showSuccessWithStatus:@"配对成功"];
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//}


#pragma mark -- 回调
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
//    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
//    if (reGeocode)
//    {
//        NSLog(@"reGeocode:%@", reGeocode);
//    }
}



- (void) layoutSubviews{
    
    
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).with.offset(64);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}


- (AMapLocationManager *)locationManager
{
    if(!_locationManager)
    {
        _locationManager = [[AMapLocationManager alloc]init];
        _locationManager.delegate = self;
    }
    return _locationManager;
}

//- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
//{
//    MAAnnotationView *annotaionView = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"aa"];
//    
//}

- (MAMapView *)mapView
{
    if(!_mapView)
    {
        UIView *superView = self.view;
        _mapView = [[MAMapView alloc]init];
        if ((DTCEXRACT_OBJECT(@"latitude") != nil) && (DTCEXRACT_OBJECT(@"longitude") != nil)) {
            CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([DTCEXRACT_OBJECT(@"latitude") floatValue], [DTCEXRACT_OBJECT(@"longitude") floatValue]);
            MAPointAnnotation *annotation = [[MAPointAnnotation alloc]init];
            annotation.coordinate = coord;
            [self.mapView addAnnotation:annotation];
            
//            _mapView.centerCoordinate = CLLocationCoordinate2DMake([DTCEXRACT_OBJECT(@"latitude") floatValue], [DTCEXRACT_OBJECT(@"longitude") floatValue]);
        }
        
        _mapView.showsUserLocation = YES;

//        _mapView.centerCoordinate = mapModel;
        [superView addSubview:_mapView];

        [_mapView setZoomLevel:16];
        [_mapView setUserTrackingMode:MAUserTrackingModeFollowWithHeading animated:YES];
    }
    return _mapView;
}

- (AMapNaviPoint *)startPoint
{
    if(!_startPoint)
    {
        _startPoint = [[AMapNaviPoint alloc]init];
    }
    return _startPoint;
}
- (AMapNaviPoint *)endPoint
{
    if(!_endPoint)
    {
        _endPoint = [[AMapNaviPoint alloc]init];
    }
    return _endPoint;
}

- (AMapNaviWalkManager *)walkManager
{
    if(!_walkManager)
    {
        _walkManager = [[AMapNaviWalkManager alloc]init];
        [_walkManager setDelegate:self];

    }
    return _walkManager;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
